package Thread;

/**
 * 这是使用join 方法
 */
public class Demo11 {
    /**
     * 关注WAITING 、 BLOCKED 、 TIMED_WAITING 状态的转换
     */
    public static void main(String[] args) {
        final  Object o = new Object();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o){
                    while (true){
                        try {
                            o.wait();
                            //Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        },"t1");
        t1.start();
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o){
                    System.out.println("略略");
                }
            }
        },"t2");
        t2.start();
    }
    /**
     * 观察NEW RUNNABLE TERMINATED状态的转换
     * @param args
     */
    public static void main3(String[] args) {
        Thread t = new Thread(()->{
            for (int i =0;i<100_0000;i++){

            }
        },"兴");
        System.out.println(t.getName()+ ":"+t.getState());
        t.start();
        while (t.isAlive()){
            System.out.println(t.getName()+":"+t.getState());
        }
        System.out.println(t.getName()+":"+t.getState());
    }
    /**
     * 观察线程的状态
     * @param args
     */
    public static void main2(String[] args) {
        for (Thread.State state : Thread.State.values()){
            System.out.println(state);
        }
    }
    public static void main1(String[] args) throws InterruptedException {
        Runnable tar = () ->{
            for (int i =0;i<10;i++){

                    System.out.println(Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println(Thread.currentThread().getName() + ": 我结束了");
        };
        Thread t1 = new Thread(tar, "兴");
        Thread t2 = new Thread(tar,"苏");
        System.out.println("先让兴开始工作");
        t1.start();
        t1.join();
        System.out.println("兴工作结束了，让苏开始工作");
        t2.start();
        t2.join();
        System.out.println("苏工作结束了");

    }
}
